﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace c1.泛型链表类
{
    internal class Program
    {
        static void Main(string[] args)
        {
            GenericList<int> list = new GenericList<int>();

            int i;
            for (i = 0; i < 10; i++)
            {
                list.Add(i);
            }

            int Min = list.Head.Data;
            int Max = list.Head.Data;
            int Sum = 0;

            Console.WriteLine("链表的节点值为：");
            list.ForEach((x) =>
            {
                Console.Write(x + " ");
                Min = Math.Min(Min, x);
                Max = Math.Max(Max, x);
                Sum += x;
            });

            Console.WriteLine();
            Console.WriteLine("链表元素的最大值为：" + Max + "，最小值为：" + Min + "，和为：" + Sum);
            Console.ReadLine();
        }
    }
}

public class Node<T>
{
    public Node<T> Next { get; set; }
    public T Data { get; set; }

    public Node(T t)
    {
        Next = null;
        Data = t;
    }
}

public class GenericList<T>
{
    private Node<T> head;
    private Node<T> tail;

    public GenericList()
    {
        tail = head = null;
    }

    public Node<T> Head
    {
        get => head;
    }

    public void Add(T t)
    {
        Node<T> n = new Node<T>(t);
        if (tail == null)
        {
            head = tail = n;
        }
        else
        {
            tail.Next = n;
            tail = n;
        }
    }

    public void ForEach(Action<T> action)
    {
        for(Node<T> n = head; n != null; n = n.Next)
        {
            action(n.Data);
        }
    }
}